import 'dart:convert';

import 'package:concierge/app/models/device_bean.dart';
import 'package:concierge/config/config.gen.dart';
import 'package:native_util/native_util.dart';

enum VType { SMS, VERTU, WECHAT, TOKEN, EMAIL, GOOGLE }

Future<String> getAuth({
  required VType vType,
  required _BaseLoginParam param,
}) async {
  return "V-${vType.name.toUpperCase()} vertu:${param.format}";
}

Future<String> getDevice() async {
  var deviceBean = DeviceBean.fromJson(await NativeUtil.getDeviceInfo());
  return jsonEncode({
    "app": yamlMap.version.split("+").first,
    "res": deviceBean.hw,
    "ver": deviceBean.ver,
    "mod": deviceBean.deviceModel,
    "sys": "Android",
    "is_no_vertu_login":
        deviceBean.deviceModel.toUpperCase().startsWith("VERTU") ? "0" : "1",
    "imei2": deviceBean.imei2,
    "rom": deviceBean.rom,
    "pg": deviceBean.pg,
    "mf": deviceBean.mf,
    "imei": deviceBean.imei,
    "sn": deviceBean.sn,
    "net": deviceBean.net,
    "did": deviceBean.did,
    "app_code": yamlMap.version.split("+").last,
  });
}

abstract class _BaseLoginParam {
  Map<String, dynamic> m = {"client": "app", "from": "app"};
  String get format =>
      m.entries.map((m) => "${m.key}=${m.value}").toList().join(";");
}

class SmsLoginParam extends _BaseLoginParam {
  SmsLoginParam._();
  static Future<SmsLoginParam> create(
    int cc,
    String mobile,
    String smsCode,
  ) async {
    final obj = SmsLoginParam._();
    obj.m["cc"] = cc;
    obj.m["mobile"] = mobile;
    obj.m["code"] = smsCode;
    obj.m["device"] = await getDevice();
    return obj;
  }
}

class VertuLoginParam extends _BaseLoginParam {
  VertuLoginParam._();
  static Future<VertuLoginParam> create(String vertuid) async {
    final obj = VertuLoginParam._();
    obj.m["vertuid"] = vertuid;
    obj.m["device"] = await getDevice();
    return obj;
  }
}

class WeChatLoginParam extends _BaseLoginParam {
  WeChatLoginParam._();
  static Future<WeChatLoginParam> create(String unionId, String openId) async {
    final obj = WeChatLoginParam._();
    obj.m["unionId"] = unionId;
    obj.m["openId"] = openId;
    obj.m["device"] = await getDevice();
    return obj;
  }
}

class EmailLoginParam extends _BaseLoginParam {
  EmailLoginParam._();
  static Future<EmailLoginParam> create(String email, String email_code) async {
    final obj = EmailLoginParam._();
    obj.m["email"] = email;
    obj.m["email_code"] = email_code;
    return obj;
  }
}

class GoogleLoginParam extends _BaseLoginParam {
  GoogleLoginParam._();
  static Future<GoogleLoginParam> create(String idToken) async {
    final obj = GoogleLoginParam._();
    obj.m["idToken"] = idToken;
    return obj;
  }
}

class TokenLoginParam extends _BaseLoginParam {
  TokenLoginParam._();
  static TokenLoginParam create(String id, String token) {
    final obj = TokenLoginParam._();
    obj.m['id'] = id;
    obj.m['token'] = token;
    return obj;
  }
}
